<template>
  <div>
    <!-- 搜索结果页-头部导航 -->
    <div class="search-result-container">
      <!-- 点击实现后退效果 -->
      <van-nav-bar title="搜索结果" left-arrow @click-left="$router.go(-1)" fixed />
    </div>
    <!-- 文章列表 -->
    <div>
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad" :immediate-check="false">
        <ArtcleItem @click.native="itemClickFn(obj.art_id)" v-for="obj in artcleList" :key="obj.art_id" :obj="obj"></ArtcleItem>
      </van-list>
    </div>
  </div>
</template>

<script>
import { searchResultListAPI } from '@/api/index.js'
import ArtcleItem from '@/components/ArtcleItem'

export default {
  name: 'SearchResult',
  components: {
    ArtcleItem
  },
  data () {
    return {
      page: 1,
      artcleList: [],
      loading: false,
      finished: false
    }
  },
  async created () {
    const { data: res } = await searchResultListAPI({
      q: this.$route.params.kw,
      page: this.page
    })
    this.artcleList = res.data.results
  },
  methods: {
    async onLoad () {
      if (this.artcleList.length > 0) {
        this.page++
        const { data: res } = await searchResultListAPI({
          q: this.$route.params.kw,
          page: this.page
        })
        if (res.data.results.length === 0) {
          // 没有更多数据
          this.finished = true
          return
        }
        this.artcleList = [...this.artcleList, ...res.data.results]

        this.loading = false
      }
    },
    itemClickFn (id) {
      this.$router.push({
        path: `/detail?art_id=${id}`
      })
    }
  }
}
</script>

<style lang="less" scoped>
.search-result-container {
  padding-top: 46px;
}
</style>
